<?php

use Swoole\Table;

// 管理所有的table
class TableStore
{
    // 存储所有的table
    static private  $store = [];
    // 新建一个table
    static public function set($tableName, $table)
    {
        self::$store[$tableName] = $table;
    }
    // 根据table名称获取table
    static public function get($tableName)
    {
        return self::$store[$tableName];
    }
}

// 创建一个新的table
class TableCreate
{
    public $table = null;
    public function init($amount, $fields)
    {
        $this->table = new Table($amount);
        foreach ($fields as $field) {
            $this->table->column(...$field);
        }
        $this->table->create();
        return $this->table;
    }
}

$table = new TableStore;

// uid table
$uidTable = (new TableCreate)->init(9000, [
    ['fd', Table::TYPE_INT, 4],
    ['site', Table::TYPE_STRING, 16],
    ['session', Table::TYPE_STRING, 128],
    ['from', Table::TYPE_STRING, 32],
    ['time', Table::TYPE_INT, 8],
]);
$table::set('uidTable', $uidTable);

// adm 
$admTable = (new TableCreate)->init(50, [
    ['fd', Table::TYPE_INT, 4],
]);
$table::set('admTable', $admTable);

// adm_online
$admOnlineTable = (new TableCreate)->init(50, [
    ['fd', Table::TYPE_INT, 4],
]);
$table::set('admOnlineTable', $admOnlineTable);


return $table;
